﻿using System;
using UnityEngine;

namespace Core.Player
{
    public class PlayerInputHandler : MonoBehaviour
    {
        private PlayerController _player;
        private Vector2 _input;

        private void Awake()
        {
            _player = GetComponent<PlayerController>();
        }

        private void Update()
        {
            if (!_player)
                return;
            
            InputHandle();
        }

        private void FixedUpdate()
        {
            _player.ControlMove(_input);
        }

        private void InputHandle()
        {
            MoveInput();
            FocusInput();
            CombatInput();
        }

        private void FocusInput()
        {
            Vector3 mousePosition = Camera.main.ScreenToWorldPoint(Input.mousePosition);
            _player.ControlRotate(mousePosition);
        }

        private void MoveInput()
        {
            float h = Input.GetAxis("Horizontal");
            float v = Input.GetAxis("Vertical");

            _input = new Vector2(h, v);
        }

        private void CombatInput()
        {
            if (Input.GetMouseButton(0))
            {
                _player.CombatCtr.Shoot(_player.transform.up);
            }
        }
    }
}